<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Model\WeChat;
use App\User;

class WeChatController extends Controller
{

    public function serve()
    {
        $app = WeChat::getWeChatOfficialAccount();

        $server = $app->server;
        $user = $app->user;

        $server->push(function($message) use ($user) {
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息';
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息'.$message['PicUrl'];
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

        $server->serve()->send();

    }

    public function createMenu()
    {
        $app = WeChat::getWeChatOfficialAccount();
        $buttons = [
            [
                "type" => "click",
                "name" => "今日歌曲",
                "key"  => "V1001_TODAY_MUSIC"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "搜索",
                        "url"  => "http://www.soso.com/"
                    ],
                    [
                        "type" => "view",
                        "name" => "视频",
                        "url"  => "http://v.qq.com/"
                    ],
                    [
                        "type" => "click",
                        "name" => "赞一下我们",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];
        $app->menu->create($buttons);
    }

    public function oauth()
    {
        $app = WeChat::getWeChatOfficialAccount();
        return $app->oauth->redirect();
    }

    public function oauth_callback()
    {
        $app = WeChat::getWeChatOfficialAccount();
        $oauth = $app->oauth;
        $weChatUserInfo  = $oauth->user();
        $user = new User();
        $user->save();

        $user_detail = new UserDeatil();
        $user_detail->user_id = $user->id;
        $user_detail->open_id = $weChatUserInfo[''];



    }


}
